package com.igeek.collateral.service.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.igeek.collateral.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

//package com.igeek.collateral.service.Impl;
//
//import com.aliyun.oss.OSS;
//import com.aliyun.oss.OSSClientBuilder;
//import com.igeek.collateral.utils.OssUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.UUID;
//
//@Service
//
@Slf4j
public class OssService {
    public static String upload(MultipartFile file) throws Exception {
        String endpoint = OssUtil.EDNPOINT;
        String accessKeyId =OssUtil.ACCESS_KEY_ID;
        String accessKeySecret =OssUtil.SECRECT;
        String bucketName =OssUtil.BUCKET;

        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        OSS ossClient = (new OSSClientBuilder()).build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);
        log.info("endpoint{}", endpoint);
        String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
//        String url = endpoint.split("//")[0] + "/"+ fileName;
        System.out.println(url);
        ossClient.shutdown();
        return url;
    }

}
